

#import "PUDTHTSelView.h"
#import "PUPDMoreCell.h"
#import "PUMoreModel.h"
#import "PUDTHTSelCell.h"

#define SCREENW     [UIScreen mainScreen].bounds.size.width
@interface PUDTHTSelView ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UITableViewDelegate,UITableViewDataSource>{
    UIView *contentView;
}
@property (nonatomic,strong) UICollectionView *collectView;
@property (nonatomic,strong) NSMutableArray *BGArray;
@property (nonatomic,retain) UIView *nohahaAlphaView;
@property (nonatomic,retain)UITableView *tableView;
@end
@implementation PUDTHTSelView


- (instancetype)initCPPDMoreViewViewWith:(NSArray *)data{

    if (self = [super init]) {
        self.BGArray=[NSMutableArray arrayWithArray:data];
        self.frame = [UIScreen mainScreen].bounds;
    }
    [self initAppreaence];
    return self;
}


-(void)moreGesture{
    [self dismiss];
}
-(void)nohahaAlphaViewGesture{
    [self dismiss];
}

#pragma mark - initAppreaence
- (void)initAppreaence {
    
    
    self.nohahaAlphaView = [[UIView alloc]init];
    self.nohahaAlphaView.frame =self.bounds;
    self.nohahaAlphaView.backgroundColor = [UIColor clearColor];
    [self addSubview:self.nohahaAlphaView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(nohahaAlphaViewGesture)];
    self.nohahaAlphaView.userInteractionEnabled=YES;
    [self.nohahaAlphaView addGestureRecognizer:tap];
    
    //375
    //402
    contentView = [[UIView alloc] init];
    contentView.frame=CGRectMake(0, self.frame.size.height, self.frame.size.width, self.frame.size.width*1.072);

    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:contentView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(20, 20)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = contentView.bounds;
    maskLayer.path = maskPath.CGPath;
    contentView.layer.mask = maskLayer;
    contentView.backgroundColor=[UIColor whiteColor];
    
   
    [self addSubview:contentView];
    
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.50];
    //添加白色view
    
//    UIView *whiteView = [[UIView alloc] init];
//    whiteView.frame=CGRectMake(0, 0, self.frame.size.width, 60);
//    whiteView.backgroundColor = [UIColor colorWithHex:0xF6F6F6];
//    whiteView.backgroundColor = [UIColor blueColor];
//    [contentView addSubview:whiteView];
    
    UILabel *lab=[[UILabel alloc]init];
    lab.frame=CGRectMake(0, contentView.frame.size.width*0.042, contentView.frame.size.width, contentView.frame.size.width*0.061);
    lab.text=@"请选择话题";
    lab.font=[UIFont systemFontOfSize:16];
    lab.textAlignment=NSTextAlignmentCenter;
    lab.textColor=[UIColor blackColor];
    [contentView addSubview:lab];
    
    
    UIButton *button=[[UIButton alloc]init];
    [button setImage:[UIImage imageNamed:@"XZHTColse"] forState:UIControlStateNormal];
    button.frame=CGRectMake(contentView.frame.size.width-contentView.frame.size.width*0.042-contentView.frame.size.width*0.064, contentView.frame.size.width*0.042, contentView.frame.size.width*0.064, contentView.frame.size.width*0.064);
//    button.frame=CGRectMake(0, contentView.frame.size.height*0.789, self.frame.size.width, contentView.frame.size.height-contentView.frame.size.height*0.789);
//    button.titleLabel.font = [UIFont systemFontOfSize: 14.0];
//    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    [button setTitle:@"取消" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(nohahaAlphaViewGesture) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:button];
    

    
    
    _tableView = [[UITableView alloc] init];
    _tableView.backgroundColor=[UIColor whiteColor];
    //解决scrollToRowAtIndexPath：滚动位置偏差问题
    _tableView.estimatedRowHeight = 0;
    _tableView.estimatedSectionFooterHeight = 0;
    _tableView.estimatedSectionHeaderHeight = 0;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;//设置分隔线样式
    _tableView.bounces = YES;
    _tableView.delegate=self;
    _tableView.dataSource=self;
    [contentView addSubview:self.tableView];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(button.mas_bottom).offset(contentView.frame.size.width*0.042);
        make.left.equalTo(self.mas_left).offset(0);
        make.right.equalTo(self.mas_right).offset(0);
        make.bottom.equalTo(self.mas_bottom).offset(0);
    }];
    
    

}

- (BOOL)isPhoneX {
    BOOL iPhoneX = NO;
//  if (UIDevice.currentDevice.userInterfaceIdiom != UIUserInterfaceIdiomPhone) {//判断是否是手机
//        return iPhoneX;
//    }
    if (@available(iOS 11.0, *)) {
        UIWindow *mainWindow = [[[UIApplication sharedApplication] delegate] window];
        if (mainWindow.safeAreaInsets.bottom > 0.0) {
            iPhoneX = YES;
        }
    }
    return iPhoneX;
}

#pragma mark - Actions
- (void)buttonClicked:(UIButton *)sender {
    [self dismiss];
}

#pragma mark - pickerView出现
- (void)show {
    
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    
    [UIView animateWithDuration:0.2 animations:^{
        
//        if ([self isPhoneX]) {
//            self->contentView.center = CGPointMake(self.frame.size.width/2, self->contentView.center.y - self->contentView.frame.size.height-44);
////            self->contentView.frameHeight=self->contentView.frameHeight+10;
//        }else{
            self->contentView.center = CGPointMake(self.frame.size.width/2, self->contentView.center.y - self->contentView.frame.size.height);
//        }
     
    }];
}

#pragma mark - pickerView消失
- (void)dismiss{
    [self->contentView removeFromSuperview];
    self->contentView=nil;
    [self removeFromSuperview];
    
//    [UIView animateWithDuration:0.1 animations:^{
//        self->contentView.center = CGPointMake(self.frame.size.width/2, self->contentView.center.y + self->contentView.frame.size.height);
//    } completion:^(BOOL finished) {
//        [self removeFromSuperview];
//    }];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return _window_width*0.192;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 6;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    

    
    static NSString *cellIdentifier1 = @"FLDTHTSelCell";//定义一个可重用标识
    PUDTHTSelCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier1];//从重用队列里获取可重用的cell
    if (cell==nil)
    {
        //如果不存在，创建一个可重用cell
        cell =[[PUDTHTSelCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier1] ;
//        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }

//    cell.textLabel.text=[NSString stringWithFormat:@"%ld",(long)indexPath.row];
    return cell;
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self dismiss];
    if ([self.delegate respondsToSelector:@selector(selectIndex:)]) {
        [self.delegate selectIndex:indexPath.row];
    }
}
@end
